Spatial Data in R

Introduction to Leaflet

Today’s agenda

Spatial Data in R

  • Data that represents physical locations on Earth.
    • Data is described by coordinates: lat / long
  • Allow us to unveil patterns and trends geographically.
  • R’s ggplot2 and ggmap offer powerful visualization tools.
  • ggplot2: Creates detailed spatial visualizations.
  • leaflet: Creates detailed interactive data viz
  • Useful in environmental studies, urban planning, and more.

The leaflet package

  • A powerful and versatile tool for creating interactive maps
  • Enables users to dynamically explore spatial data
  • Offers extensive options for custom markers, layers, etc.
  • Maps: Variety of cartography (providerTiles)
    • Points/Markers
    • Polygons
    • Lines

The leaflet package

  • Tiles are the cartography loaded from a server
  • They can of different styles (satellite, terrain, roadmaps)
## Creating our first map
# Adding the first layer: the tiles
leaflet() |>  
  addTiles()

The leaflet package

## Creating our first map
# Adding a second layer: a `marker` is a single point in the graph!
leaflet() |> 
  addTiles() |> 
  addMarkers(lng = 174.768,
             lat = -36.852,
             popup = "Birthplace of R")

The leaflet package

  • Changing to a lighter version of tile
## Creating our first graph
# Changing the tile
leaflet() |> 
  addProviderTiles("CartoDB") |> 
  addMarkers(lng = 174.768,
             lat = -36.852,
             popup = "Birthplace of R")

The leaflet package

  • CircleMarkers: adding a different type of marker
## Creating our first graph
# Adding circle markers
leaflet() |> 
  addProviderTiles("CartoDB") |> 
  addMarkers(lng = 174.768,
             lat = -36.852,
             popup = "Birthplace of R") |>
  addCircleMarkers(lng = 174.778, lat = -36.854,
                   radius = 10,
                   color = "red",
                   label = "A geolocation close to R's Birthplace")

The leaflet package

  • setView: setting the default options of the map
  • leafletOptions: choosing the user possibilities
## Creating our first graph
# Settting some user options
leaflet(options = leafletOptions(minZoom = 10, dragging = TRUE)) |> 
  addProviderTiles("CartoDB") |> 
  addMarkers(lng = 174.768,
             lat = -36.852,
             popup = "Birthplace of R") |>
  addCircleMarkers(lng = 174.778, lat = -36.854,
                   radius = 10,
                   color = "red",
                   label = "A geolocation close to R's Birthplace") |> 
  setView(lng = 174.8,
          lat = -36.9,
          zoom = 8)

The leaflet package

  • Adding data to our map!
  • Using the lat and long parameters
  • Markers, circles, pins’s functions all have them
## Loading our data
# Condition1 1: the data need to have the lat and long columns
cps_schools <- read_csv('cps_schools_database.csv')
head(cps_schools, 5)
# A tibble: 5 × 4
  school_id school_nm                      school_lat school_long
      <dbl> <chr>                               <dbl>       <dbl>
1    400009 GLOBAL CITIZENSHIP CHTR              41.8       -87.7
2    400010 ACE TECHNICAL CHTR HS                41.8       -87.6
3    400011 LOCKE, A CHTR                        41.9       -87.7
4    400012 AMANDLA CHTR HS                      41.8       -87.6
5    400013 ASPIRA CHTR - EARLY COLLEGE HS       41.9       -87.7

The leaflet package

## Creating our first graph
# Linking the lat/long columns and adding a label to the circle markers
cps_schools |> 
  leaflet() |> 
  addProviderTiles("CartoDB") |> 
  addCircleMarkers(lng = ~school_long,
                   lat = ~school_lat,
                   radius = 5,
                   color = "red",
                   label = ~school_nm)

Lecture recap

  • leaflet: we can use for creating interactive maps, enhancing spatial data exploration.
  • highly customizable: add tiles, markers, and circle markers, set the view, etc.
  • different features: adding various layers (tiles, markers)
  • light and easy to work with (shorter lines of code)

Stay tuned!